let isShowIng = false;
let showTimer, closeTimer;


function show() {

	if (isShowIng) {
		return;
	}
	if (showTimer) {
		clearTimeout(showTimer);
	}

	showTimer = setTimeout(() => {
		uni.showLoading({
			title: '加载中...',
			mask: true,
			complete: () => {
				isShowIng = true;
			}
		})
	}, 500);

	if (closeTimer) {
		clearTimeout(closeTimer)
	}
	closeTimer = setTimeout(() => {
		if (isShowIng) {
			uni.hideLoading();
		}
	}, 10 * 1000);

}

function hide() {
	if (isShowIng) {
		uni.hideLoading();
	}

	if (showTimer) {
		clearTimeout(showTimer);
	}
	isShowIng = false;
}

export const setStorage = (key,value)=>{
	//new Promise((resolve,reject) =>{
		uni.setStorageSync(key, value);
	//})
}

export const getStorage = (key)=>{
	return uni.getStorageSync(key) || '';
}

export const toast = (title,icon='none',duration=2000)=>{
	uni.showToast({
		title,
		icon,
		duration
	});
}

const zeroFill = (num)=> {
	return num < 10 ? '0' + num : num;
}

export const dateFormat = (fmt, time)=> {
	let date = new Date(time);
	// if (time) {
	// 	date.setTime(time);
	// }

	let o = {
		"Y": date.getFullYear(),
		"m": zeroFill(date.getMonth() + 1),
		"d": zeroFill(date.getDate()),
		"H": zeroFill(date.getHours()),
		"i": zeroFill(date.getMinutes()),
		"s": zeroFill(date.getSeconds()),
	};
	for (let k in o) {
		if (fmt.indexOf(k) > -1) {
			fmt = fmt.replace(new RegExp("(" + k + ")"), o[k]);
		}
	}
	return fmt;
}

// 日期加减：返回time加上num天后的时间
export const dateAdd = (num,time)=> {
	// 创建一个表示当前时间的 Date 对象
	let startTime = new Date();
	if(time){
		startTime = new Date(time);
	}
	 
	// 设置需要添加/减少的天数
	let daysToAddOrSubtract = num
	 
	// 根据需求选择相应的操作符
	if (daysToAddOrSubtract > 0) {
	    // 若需要向未来添加天数，则直接调用 setDate() 函数
	    startTime.setDate(startTime.getDate() + daysToAddOrSubtract);
	} else if (daysToAddOrSubtract < 0) {
	    // 若需要向过去倒退天数，则先将天数转换成正值再调用 setDate() 函数
	    let absDaysToSubtract = Math.abs(daysToAddOrSubtract);
	    startTime.setDate(startTime.getDate() - absDaysToSubtract);
	}
	return startTime
}


export const getWeekday = dateString=> {
  const date = new Date(dateString);
  const weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  return weekdays[date.getDay()];
}

export const checkMobile = phone =>{
    const myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!myreg.test(phone)) {
        return false;
    } else {
        return true;
    }
}

export const serviceStatus = (startTime,endTime) =>{
    if(!startTime || !endTime){
		return '未知'
	}
	const nowStamp = new Date().getTime()
	const stamp1 = new Date(startTime).getTime()
	const stamp2 = new Date(endTime).getTime()
	// 这里只判断未开始和进行中两个状态，完成状态是需要教师手动点击完成操作并后台更改订单状态
	if(nowStamp<stamp1){
		return '未开始'
	}else{
		return '进行中'
	}
	
	// else if(nowStamp>=stamp1 && nowStamp<=stamp2){
	// 	return '进行中'
	// }else{
	// 	return '已完成'
	// }
}

// 比较两个时间大小
export const compareTime = (time1,time2) =>{
    if(!time1 || !time2){
		return '--'
	}
	const stamp1 = new Date(time1).getTime()
	const stamp2 = new Date(time2).getTime()
	if(stamp1>stamp2){
		return 1
	}else if(stamp1==stamp2){
		return 0
	}else{
		return -1
	}
}





export default {
	show: show,
	hide: hide,
}
